﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using InvoiceProject.Helpers;

namespace InvoiceProject.Models
{
    public class Address : BaseModel, IValidatableObject
    {

       
        [Required]
        [Display(Name = "Address Line 1")]
        public string AddressLine1 { get; set; }

        [Display(Name = "Address Line 2")]
        public string AddressLine2 { get; set; }

        [Required]
        public string Suburb { get; set; }

        [Required]
        public string State { get; set; }
        
        [Required]
        public int PostCode { get; set; }

        public  new IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (String.IsNullOrEmpty(AddressLine1))
              yield return new ValidationResult("Address Line 1, is null");

            if (String.IsNullOrEmpty(Suburb))
                yield return new ValidationResult("Suburb, is null");

            if (PostCode < 1000 || PostCode > 9999 || PostCode==0)
                 yield return new ValidationResult("Postcode Must be 4 Digits");

            if (String.IsNullOrEmpty(State))
                 yield return new ValidationResult("State, is null");

        }
       

        public List<ValidationError> Validate()
        {
            var errorList = new List<ValidationError>();

            if (String.IsNullOrEmpty(AddressLine1))
                errorList.Add(new ValidationError("Address Line 1, is null"));

            if (String.IsNullOrEmpty(Suburb))
                errorList.Add(new ValidationError("Suburb, is null"));

            if (PostCode> 999 && PostCode >=9999)
                errorList.Add(new ValidationError("Postcode Must be 4 Digits"));

            if (String.IsNullOrEmpty(State))
                errorList.Add(new ValidationError("State, is null"));

   
            return errorList;
         }

        public bool IsValid()
        {
            return Validate().Count == 0;
        }


    }



}